package appClientModule;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

public class MsgGenUtil {
	private static int iSerial;
	public static final String[] strTmp;
	static {
		iSerial = 0;
		strTmp = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
	}

	public MsgGenUtil() {
	}

	public static byte[] appendByte(byte abyte0[], byte byte0) {
		byte abyte1[] = new byte[abyte0.length + 1];
		System.arraycopy(abyte0, 0, abyte1, 0, abyte0.length);
		abyte1[abyte0.length] = byte0;
		return abyte1;
	}

	public static String b2bin(byte byte0) {
		String s = Integer.toBinaryString(byte0 & 0xff);
		String s1 = s;
		for (int i = 0; i < 8 - s.length(); i++)
			s1 = "0" + s1;
		return s1;
	}

	public static String b2hex(byte byte0) {
		String s = Integer.toHexString(byte0 & 0xff);
		String s1;
		if (s.length() == 1)
			s1 = "0".concat(s);
		else
			s1 = s;
		return s1.toUpperCase();
	}

	/*
	 * String->byte[]
	 */
	public static byte[] BCD2Bytes(String s) {
		byte[] returnBytes;
		int nlen = (s.length() + 1) / 2;
		returnBytes = new byte[nlen];//
		for (int i = 0; i < nlen; i++) {
			int temp = Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
			returnBytes[i] = new Integer(temp).byteValue();

		}
		return returnBytes;
	}

	public static String BCDAdd(String bcd, int j) {
		int i = Integer.parseInt(bcd, 16) + j;
		String s = Integer.toHexString(i);
		while (s.length() % 2 == 1 || s.length() < 4)
			s = "0" + s;
		return s;
	}

	public static String buf2hex(byte abyte0[]) {
		String s = new String();
		for (int i = 0; i < abyte0.length; i++)
			s = s.concat(b2hex(abyte0[i]));

		s = s.toUpperCase();
		// s= s.toLowerCase();
		return s;
	}

	public static String buf2hex(byte abyte0[], int len) {
		String s = new String();
		for (int i = 0; i < len; i++)
			s = s.concat(b2hex(abyte0[i]));

		// s = s.toUpperCase();
		s = s.toLowerCase();
		return s;
	}

	public static byte[] concat(byte abyte0[], byte abyte1[]) {
		byte abyte2[] = new byte[abyte0.length + abyte1.length];
		System.arraycopy(abyte0, 0, abyte2, 0, abyte0.length);
		System.arraycopy(abyte1, 0, abyte2, abyte0.length, abyte1.length);
		return abyte2;
	}

	public static byte[] getSerial() {
		iSerial++;
		if (iSerial > 0x7FFFFFFF) {
			iSerial = 0;
			System.out.println(iSerial);
		}
		byte[] target = new byte[4];
		IntToBytes(iSerial, target);
		return target;
	}

	public static byte[] Int2Bytes(int source) {
		byte[] target = new byte[4];
		for (int i = 0; i < 4; i++) {
			target[3 - i] = (byte) ((source >> (i * 8)) & 0x000000FF);
		}
		return target;
	}

	public static byte[] Int2Bytes3(int source) {
		byte[] target = new byte[3];
		for (int i = 0; i < 3; i++) {
			target[2 - i] = (byte) ((source >> (i * 8)) & 0x000000FF);
		}
		return target;
	}

	public static byte[] Long2Bytes5(long source) {
		byte[] target = new byte[5];
		for (int i = 0; i < 5; i++) {
			target[4 - i] = (byte) ((source >> (i * 8)) & 0x000000FFl);
		}
		return target;
	}

	public static byte[] Int2Bytes2(int source) {
		byte[] target = new byte[2];
		for (int i = 0; i < 2; i++) {
			target[1 - i] = (byte) ((source >> (i * 8)) & 0x000000FF);
		}
		return target;
	}

	public static byte[] Int2Bytes1(int source) {
		byte[] target = new byte[1];

		target[0] = (byte) (source & 0x000000FF);

		return target;
	}

	public static String int2s(int i) {
		String s = "" + i;
		if (i < 10)
			s = "0" + s;
		return s;
	}

	public static byte[] intStr2Bytes(String i) {
		int it = Integer.parseInt(i);
		String s = Integer.toHexString(it);
		if (s.length() % 2 == 1)
			s = "0" + s;
		byte[] rt = BCD2Bytes(s);
		return rt;
	}

	public static void IntToBytes(int source, byte[] target) {
		for (int i = 0; i < 4; i++) {
			target[3 - i] = (byte) ((source >> (i * 8)) & 0x000000FF);
		}
	}

	public static int StrToInt(String scr, byte[] tar) {
		int nLen = scr.length(), sum = 0;
		byte[] middle;
		try {
			middle = scr.getBytes("UTF-8");
		} catch (Exception err) {
			err.printStackTrace();
			return 0;
		}
		for (int i = 0; i < nLen; i++) {
			sum = sum * 10 + (middle[i] - '0');
		}
		IntToBytes(sum, tar);
		return sum;
	}

	public static int makeInt(byte abyte0[]) {
		return Integer.parseInt(buf2hex(abyte0), 16);
	}

	public static byte[] now2Bytes() {
		String s = "";
		Calendar now = new GregorianCalendar();
		Date trialTime = new Date();
		now.setTime(trialTime);

		s += int2s(now.get(Calendar.MONTH) + 1);
		s += int2s(now.get(Calendar.DATE));
		s += int2s(now.get(Calendar.HOUR_OF_DAY));
		s += int2s(now.get(Calendar.MINUTE));
		s += int2s(now.get(Calendar.SECOND));

		return intStr2Bytes(s);
	}

	public static byte[] padding(byte abyte0[], byte byte0, int i) {
		byte abyte1[] = new byte[abyte0.length + i];
		System.arraycopy(abyte0, 0, abyte1, 0, abyte0.length);
		for (int j = 0; j < i; j++)
			abyte1[j + abyte0.length] = byte0;

		return abyte1;
	}

	public static String replace(String src, String find, String rp) {
		int i = src.indexOf(find);
		if (i < 0)
			return src;
		String a = src.substring(0, i);
		String b = src.substring(i + find.length(), src.length());
		return a + rp + b;
	}

	public static String replaceAll(String src, String find, String rp) {
		String a = "";
		String b = "";
		if (src.startsWith(find))
			a = rp;
		if (src.endsWith(find))
			b = rp;
		StringTokenizer st = new StringTokenizer(src, find);
		String t, rt;
		rt = "";
		while (st.hasMoreTokens()) {
			t = st.nextToken();
			rt = rt + t + rp;
		}
		rt = a + rt.substring(0, rt.lastIndexOf(rp)) + b;
		return rt;
	}

	public static String toUsc2(String s) throws Exception {
		try {
			String a = buf2hex(s.getBytes("UnicodeBig"));
			if (a.length() > 4)
				return a.substring(4);
			else
				return a;
		} catch (Exception ex) {
			return "";
		}
	}

	public static void fillbyte(byte[] a, byte b) {
		for (int i = 0; i < a.length; i++)
			a[i] = b;

	}

	public static int getByteTrimLen(byte src[]) {
		try {
			String temp = new String(src, "UTF-8");
			return temp.trim().length();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	public static int getIndexByteLen(byte src[], int offset) {
		char zero = 0x0000;
		@SuppressWarnings("unused")
		int count = 0, len = 0;
		@SuppressWarnings("unused")
		int j = 0, i = 0;
		try {
			String temp = new String(src, "UTF-8");
			if (src[offset] != 0x00) {
				for (j = offset; j < temp.length(); j++) {
					if ((j > 0) && (temp.charAt(j) == zero) && (temp.charAt(j - 1) != zero))
						break;
				}
				if (j < temp.length())
					return temp.substring(offset, j).length() + 1;
				else
					return 1;
			} else
				return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	public static String toGBK(String src) {
		byte[] temp = null;
		try {
			temp = src.getBytes("utf-16");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return "80" + buf2hex(temp).replaceFirst("FEFF", "");

	}

	public static void concatBCD(String strParam, String tag, StringBuffer buffer) {
		buffer.append(tag);
		byte[] param = MsgGenUtil.BCD2Bytes(strParam);
		int len = strParam.length() / 2 + 8;
		if (len < 256) {
			buffer.append(MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(len)));
		} else if ((len >= 128) && (len < 256)) {
			buffer.append("81");
			buffer.append(MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(len)));
		} else if (len >= 256) {
			buffer.append("82");
			buffer.append(MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes2(len)));
		}
		String tmp = MsgGenUtil.buf2hex(param);

		buffer.append(tmp);
	}

	public static void concatBCD(byte[] strParam, String tag, StringBuffer buffer) {
		buffer.append(tag);
		byte[] param = strParam;
		int len = param.length;
		if (len < 128) {
			buffer.append(MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(len)));
		} else if ((len >= 128) && (len < 256)) {
			buffer.append("81");
			buffer.append(MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(len)));
		} else if (len >= 256) {
			buffer.append("82");
			buffer.append(MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes2(len)));
		}
		String tmp = MsgGenUtil.buf2hex(param);

		buffer.append(tmp);
	}

	public static void concat(String strParam, String tag, StringBuffer buffer) {
		buffer.append(tag);
		byte[] param = strParam.getBytes();
		int len = strParam.length();
		if (len < 128) {
			buffer.append(MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(len)));
		} else if ((len >= 128) && (len < 256)) {
			buffer.append("81");
			buffer.append(MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes1(len)));
		} else if (len >= 256) {
			buffer.append("82");
			buffer.append(MsgGenUtil.buf2hex(MsgGenUtil.Int2Bytes2(len)));
		}
		String tmp = MsgGenUtil.buf2hex(param);

		buffer.append(tmp);
	}

	// lengthType 0:数据长度 1:表示数据长度的字节长�?1�?7
	public static int getLengthFromBERTLV(String commondData, int lengthType) {
		int returnLength = 0;
		int returnLengthLen = 0;
		String byte1 = commondData.substring(0, 2);
		int byte1Int = Integer.parseInt(byte1, 16);
		if (byte1Int < 128) {
			returnLength = byte1Int;
			returnLengthLen = 1;
		} else if (byte1Int == 129) {
			String byte2 = commondData.substring(2, 4);
			int byte2Int = Integer.parseInt(byte2, 16);
			returnLength = byte2Int;
			returnLengthLen = 2;
		} else if (byte1Int == 130) {
			String byte3 = commondData.substring(2, 6);
			int byte3Int = Integer.parseInt(byte3, 16);
			returnLength = byte3Int;
			returnLengthLen = 3;
		}
		if (lengthType == 0)
			return returnLength * 2;
		else
			return returnLengthLen * 2;
	}

	public static byte[] halfNumber(String src) {
		int srcLen = src.length();
		byte[] result = new byte[(srcLen + 1) / 2];
		if (srcLen % 2 != 0) {
			src += "F";
		}
		result = MsgGenUtil.BCD2Bytes(src);
		for (int i = 0; i < result.length; i++) {
			result[i] = changePlace(result[i]);
		}
		return result;
	}

	public static byte changePlace(byte src) {
		int high = src & 0xf0;
		int low = src & 0x0f;
		return (byte) ((high >> 4) + (low << 4));
	}

	public static String exchangePlace(String src) {
		int srcLen = src.length();
		if (srcLen % 2 != 0) {
			src += "F";
		}
		StringBuffer buff = new StringBuffer();
		for (int i = 0; i < src.length();) {
			buff.append(src.charAt(i + 1));
			buff.append(src.charAt(i));
			i += 2;
		}
		return buff.toString();
	}

	public static String encodeXOR(String str, String key) {
		String result = null;
		byte[] strBuf = MsgGenUtil.BCD2Bytes(str);
		byte[] keyBuf = MsgGenUtil.BCD2Bytes(key);
		int c = 0;
		int z = keyBuf.length;
		ByteArrayOutputStream baos = new ByteArrayOutputStream(strBuf.length);
		for (int i = 0; i < strBuf.length; i++) {
			byte bS = strBuf[i];
			byte bK = keyBuf[c];
			byte bO = (byte) (bS ^ bK);
			if (c < z - 1) {
				c++;
			} else {
				c = 0;
			}
			baos.write(bO);
		}
		try {
			baos.flush();
			result = MsgGenUtil.buf2hex(baos.toByteArray());
			baos.close();
			baos = null;
		} catch (IOException io) {
			io.getStackTrace();
		}
		return result;
	}

	public static void main(String args[]) {
		String a = "9814001560110176";
		String b = "0015601101760020";

		System.out.println(encodeXOR(a, b));

	}
}
